package au.com.handsunited.web.controller.group;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import au.com.handsunited.model.Group;
import au.com.handsunited.service.GroupService;
import au.com.handsunited.web.command.group.GroupSearchCommand;

/**
 * @author arunda
 *
 */
public class GroupSearchController extends SimpleFormController {


	private GroupService groupService;

	private static Logger logger = LoggerFactory.getLogger(GroupSearchController.class);

	/**
	 *
	 */
	public GroupSearchController() {
		setCommandClass(GroupSearchCommand.class);
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.validation.BindException)
	 */
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		GroupSearchCommand groupSearchCommand = (GroupSearchCommand) command;

		ModelAndView mav = new ModelAndView(
				getSuccessView(), getCommandName(), groupSearchCommand);

		List<Group> groups = groupService.searchForGroups(
				groupSearchCommand.getKeywords(), groupSearchCommand.getName());

		mav.addObject("groups", groups);

		return mav;
	}


	/**
	 * @param groupService the groupService to set
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

}
